package com.fh.fruit.service.plantbase.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.fruit.entity.plantbase.PlantBase;
import com.fh.fruit.service.plantbase.PlantBaseManager;
import com.fh.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 种植基地
 * 
 * @author Administrator
 * @version 2017.11.30 15:39
 */
@Service("plantBaseService")
public class PlantBaseService implements PlantBaseManager {
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	@Override
	@SuppressWarnings("unchecked")
	public List<PlantBase> listPlantBases(Page page) throws Exception {
		return (List<PlantBase>) dao.findForList("PlantBaseMapper.plantBaseslistPage", page);
	}

	@Override
	public void savePlantBases(PageData pd) throws Exception {
		dao.save("PlantBaseMapper.savePlantBase", pd);
	}

	@Override
	public int deletePlantBases(PageData pd) throws Exception {
		return (int) dao.delete("PlantBaseMapper.delPlantBases", pd);
	}

	@Override
	public int updatePlantBases(PageData pd) throws Exception {
		return (int) dao.update("PlantBaseMapper.updatePlantBase", pd);
	}

	@Override
	public PlantBase findById(PageData pd) throws Exception {
		return (PlantBase) dao.findForObject("PlantBaseMapper.findById", pd);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<PageData> findByNameOrCode(PageData pd) throws Exception {
		return (List<PageData>) dao.findForList("PlantBaseMapper.findByNameOrCode", pd);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<PageData> synTree(int parseInt) throws Exception {
		return (List<PageData>) dao.findForList("PlantBaseMapper.synTree", parseInt);
	}
	@Override
	@SuppressWarnings("unchecked")
	public List<PageData> list(PageData pd) throws Exception {
		return (List<PageData>) dao.findForList("PlantBaseMapper.regionList", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> listDeliveryAdd(Page page) throws Exception {
		return (List<Map<String, Object>>) dao.findForList("PlantBaseMapper.deliverylistPage", page);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getAddress(PageData pd) throws Exception {
		return (Map<String,Object>)dao.findForObject("PlantBaseMapper.getAddress", pd);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getBaseMsg(PageData pd) throws Exception {
		return (Map<String,Object>)dao.findForObject("PlantBaseMapper.getBaseMsg", pd);
	}

	@Override
	public int editD(PageData pd) throws Exception {
		int num=1;
		String state=(String)pd.get("state");
		try {
			if("01".equals(state)){
				dao.update("PlantBaseMapper.updateAddStatus", pd);
			}
			num=(int)dao.update("PlantBaseMapper.editD", pd);
			if(num==0){
				return num;
			}
		} catch (Exception e) {
			e.printStackTrace();
			num=0;
			return num;
		}finally {
			if(num!=1){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
		return num;
	}

	@Override
	public void saveD(PageData pd) throws Exception {
		String state=(String)pd.get("state");
		if("01".equals(state)){
			dao.update("PlantBaseMapper.updateAddStatus", pd);
		}
		dao.save("PlantBaseMapper.saveD", pd);
	}

	@Override
	public int deleteD(PageData pd) throws Exception {
		return (int) dao.delete("PlantBaseMapper.deleteD", pd);
	}

}
